#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
战场布局可视化
"""

from game.game_manager import GameManager

def visualize_battlefield():
    """可视化战场布局"""
    print("=== 回合制战斗游戏 - 新战场布局 ===")
    print()
    
    # 创建游戏管理器并设置战斗
    game_manager = GameManager()
    battle_id = game_manager.setup_sample_battle()
    
    # 获取战斗状态
    battle_state = game_manager.get_battle_state()
    
    if not battle_state:
        print("无法获取战斗状态")
        return
    
    # 创建战场网格
    width = battle_state['battlefield']['width']
    height = battle_state['battlefield']['height']
    
    # 初始化网格
    grid = [['.' for _ in range(width)] for _ in range(height)]
    
    # 放置我方角色
    player_chars = {char['id']: char for char in battle_state['player_characters']}
    for char_id, pos in battle_state['battlefield']['player_positions'].items():
        char = player_chars[char_id]
        x, y = pos['x'], pos['y']
        # 使用角色名称的首字母
        grid[y][x] = char['name'][0]
    
    # 放置敌方角色
    enemy_chars = {char['id']: char for char in battle_state['enemy_characters']}
    for char_id, pos in battle_state['battlefield']['enemy_positions'].items():
        char = enemy_chars[char_id]
        x, y = pos['x'], pos['y']
        # 使用小写字母表示敌方
        grid[y][x] = char['name'][0].lower()
    
    # 打印战场
    print("战场布局 (4x6网格):")
    print("上方区域 (y=0-2): 敌方B队 (小写字母)")
    print("下方区域 (y=3-5): 我方A队 (大写字母)")
    print()
    
    # 打印坐标轴
    print("   ", end="")
    for x in range(width):
        print(f" {x} ", end="")
    print()
    
    # 打印网格
    for y in range(height):
        print(f"{y}: ", end="")
        for x in range(width):
            cell = grid[y][x]
            if cell == '.':
                print(" · ", end="")
            else:
                print(f" {cell} ", end="")
        
        # 添加区域标识
        if y <= 2:
            print("  ← 敌方区域 (B队)", end="")
        else:
            print("  ← 我方区域 (A队)", end="")
        print()
    
    print()
    
    # 打印角色详情
    print("=== 角色详情 ===")
    print("\n我方A队:")
    for char in battle_state['player_characters']:
        pos = battle_state['battlefield']['player_positions'][char['id']]
        print(f"  {char['name']} ({char['name'][0]}) - 位置: ({pos['x']}, {pos['y']}) - HP: {char['hp']} - 攻击: {char['attack']}")
    
    print("\n敌方B队:")
    for char in battle_state['enemy_characters']:
        pos = battle_state['battlefield']['enemy_positions'][char['id']]
        print(f"  {char['name']} ({char['name'][0].lower()}) - 位置: ({pos['x']}, {pos['y']}) - HP: {char['hp']} - 攻击: {char['attack']}")
    
    print()
    print("=== 战场规则 ===")
    print("• 战场尺寸: 4x6 (宽x高)")
    print("• 敌方B队区域: y坐标 0-2 (上方3行)")
    print("• 我方A队区域: y坐标 3-5 (下方3行)")
    print("• 角色只能在各自区域内移动和放置")
    print("• 技能可以跨区域攻击")
    
    return battle_id

def demonstrate_range_attacks():
    """演示跨区域攻击"""
    print("\n=== 跨区域攻击演示 ===")
    
    # 模拟一些攻击距离
    print("\n距离计算示例:")
    print("我方战士 (1,3) 到 敌方哥布林战士 (1,2) 的距离: 1格 (可近战攻击)")
    print("我方法师 (0,4) 到 敌方哥布林法师 (2,1) 的距离: 5格 (需要远程技能)")
    print("火球术范围攻击 (1格范围) 可以同时攻击相邻的多个敌人")
    print("群体治疗 (2格范围) 可以治疗范围内的多个队友")

def show_skill_ranges():
    """显示技能范围"""
    print("\n=== 技能范围说明 ===")
    
    game_manager = GameManager()
    skills = game_manager.get_available_skills()
    
    for skill_name, skill_info in skills.items():
        target_type = skill_info.get('target_type', '未知')
        skill_type = skill_info.get('skill_type', '未知')
        
        if 'range' in skill_info:
            range_info = f"范围: {skill_info['range']}格"
        else:
            range_info = "单体目标"
        
        print(f"• {skill_name}: {skill_type} - {target_type} - {range_info}")

if __name__ == "__main__":
    battle_id = visualize_battlefield()
    demonstrate_range_attacks()
    show_skill_ranges()
    
    print(f"\n战斗ID: {battle_id}")
    print("\n🎉 新的战场布局已成功实现！")